﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System.Collections.Generic;

namespace AbstractClass.Collections.Generic
{
    /// <summary>
    /// Compares two <see cref="KeyValuePair{TKey,TValue}"/> objects for equivalence.
    /// </summary>
    /// <typeparam name="TKey">The type of the key in the <see cref="KeyValuePair&lt;TKey, TValue&gt;"/> object.</typeparam>
    /// <typeparam name="TValue">The type of the value in the <see cref="KeyValuePair&lt;TKey, TValue&gt;"/> object.</typeparam>
    public class KeyValueComparer<TKey, TValue> : IComparer<KeyValuePair<TKey, TValue>>, IComparer<TKey>
    {
        private readonly IComparer<TKey> _comparer;

        /// <summary>
        /// Initializes a new instance of the <see cref="KeyValueComparer&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        public KeyValueComparer()
        {
            _comparer = Comparer<TKey>.Default;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="KeyValueComparer&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        /// <param name="comparer">The comparer.</param>
        public KeyValueComparer(IComparer<TKey> comparer)
        {
            _comparer = comparer;
        }

        #region Implementation of IComparer<KeyValuePair<TKey,TValue>>

        /// <summary>
        /// Performs a case-sensitive comparison of two <see cref="KeyValuePair&lt;TKey, TValue&gt;"/> objects 
        /// and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// <returns>
        /// A signed integer that indicates the relative values of x and y, as shown in the following table
        /// <list type="">
        /// <item>Less than zero : x is less than y</item>
        /// <item>Zero : x is equal to y</item>
        /// <item>Greater than zero : x is greater than y</item>
        /// </list>
        /// </returns>
        public int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y)
        {
            return _comparer.Compare(x.Key, y.Key);
        }

        #endregion

        #region Implementation of IComparer<TKey>

        /// <summary>
        /// Performs a case-sensitive comparison of two <typeparamref name="TKey"/> of two <see cref="KeyValuePair&lt;TKey, TValue&gt;"/> objects 
        /// and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// <param name="x">The first key to compare.</param>
        /// <param name="y">The second key to compare.</param>
        /// <returns>
        /// A signed integer that indicates the relative values of x and y, as shown in the following table.
        /// <list type="">
        /// <item>Less than zero : x is less than y</item>
        /// <item>Zero : x is equal to y</item>
        /// <item>Greater than zero : x is greater than y</item>
        /// </list>
        /// </returns>
        public int Compare(TKey x, TKey y)
        {
            return _comparer.Compare(x, y);
        }

        #endregion
    }
}